package sdk.webview.fmc.com.fmcsdk.view;

/**
 * Created by ydm on 2018/8/20.
 */

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import sdk.webview.fmc.com.fmcsdk.bean.SystemSetting;
import sdk.webview.fmc.com.fmcsdk.util.PreferencesHelper;

/**
 * 可以切换主题色的LinearLayout
 * Created by ydm on 2017/9/4.
 */

public class LoginFrameStyleColorLinearLayout extends LinearLayout {

    private int[] colors;

    public LoginFrameStyleColorLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LoginFrameStyleColorLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public LoginFrameStyleColorLinearLayout(Context context) {
        super(context);

    }

    @Override
    protected void onAttachedToWindow() {
        GradientDrawable bg;
        SystemSetting systemSetting = PreferencesHelper.getData(SystemSetting.class);
        //如果没有系统设置数据，使用默认的颜色
        if (systemSetting==null){
            colors = new int[]{0XFFDBDEE5, 0XFF989EAA};
            bg = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,colors);
        }else {
            //设置渐变色
            String mobile_title_left_color = systemSetting.getRecord().getMOBILE_TITLE_LEFT_COLOR();
            String mobile_title_right_color = systemSetting.getRecord().getMOBILE_TITLE_RIGHT_COLOR();
            if (!TextUtils.isEmpty(mobile_title_left_color)&&!TextUtils.isEmpty(mobile_title_right_color)){
                colors = new int[]{Color.parseColor(mobile_title_left_color), Color.parseColor(mobile_title_right_color)};
            }else {
                colors = new int[]{0XFFDBDEE5, 0XFF989EAA};
            }
            bg = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,colors);
        }

        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            setBackgroundDrawable(bg);
        } else {
            setBackground(bg);
        }

        super.onAttachedToWindow();
    }
}